<?php
/**
 * ============================================================================
 * 版权所有 多多科技，保留所有权利。
 * 网站地址: http://soft.duoduo123.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
*/
if(!defined('INDEX')){
	exit('Access Denied');
}
if($dduser['id']==0 || empty($dduser['id'])){
	jump(wap_l('user','login'),'请先登录');
}
function act_m_view($pagesize=12,$field="id,title,img,jifenbao,jifen,num,`limit`,content,sdate,edate"){
	global $duoduo;
	$webset=$duoduo->webset;
	$dduser=$duoduo->dduser;
	$msgset=dd_get_cache('msgset');
	
	if(($webset['sms']['open']==1 && $webset['sms']['need_yz']==1) && ($msgset[4]['sms_open']==1 || $msgset[5]['sms_open']==1) && $dduser['mobile_test']==0){ //提现短信开启
		$huan_sms_open=1;
	}
	else{
		$huan_sms_open=0;
	}
	
	if($huan_sms_open==1 && $dduser['id']>0 && ($dduser['mobile']=='' || $dduser['mobile_test']==0)){
		jump(u('user','info',array('do'=>'mobile')),'请验证您的手机号码');
	}
	
	//类型
	$type_all=dd_get_cache('type');
	$huan_type=$type_all['huan_goods'];
	foreach($huan_type as $k=>$v){
		$huan_type_arr[$k]['url']=u('huan','list',array('cid'=>$k));
		$huan_type_arr[$k]['title']=$v;
	}
	$id =(int)$_GET['id'];
	$good=$duoduo->select('huan_goods',$field,'id="'.$id.'"');
	$good['_jifenbao']=jfb_data_type($good['jifenbao']);
	$good['_sdate']=date('Y-m-d',$good['sdate']);
	$good['_edate']=date('Y-m-d',$good['edate']);
	if($good['limit']==0){ //兑换无限制，最多兑换商品全部数量
		$good['limit']=$good['num'];
	}
	else{
		if($good['limit']<=$good['num']){ //兑换限制比商品总数少
			$good['limit']=$good['limit'];
		}
		else{
			$good['limit']=$good['num'];
		}
	}
	
	$jifenbao_dh_status=1;
	$jifen_dh_status=1;
	$jifenbao_dh_msg=TBMONEY.'兑换';
	$jifen_dh_msg='积分兑换';
	
	if($good['num']<=0){
		$jifenbao_dh_status=0;
		$jifen_dh_status=0;
		$jifenbao_dh_msg='暂无库存';
		$jifen_dh_msg='暂无库存';
	}
	
	if($dduser['id']>0){
		if($dduser['dhstate']==1){
			$jifenbao_dh_status=0;
			$jifenbao_dh_msg='您提交的兑换申请正在处理中';
			$jifen_dh_status=0;
			$jifen_dh_msg='您提交的兑换申请正在处理中';
		}
		else{
			if($dduser['live_jifenbao']<$good['jifenbao']){
				$jifenbao_dh_status=0;
				$jifenbao_dh_msg='您的'.TBMONEY.'不足';
			}
			if($dduser['live_jifen']<$good['jifen']){
				$jifen_dh_status=0;
				$jifen_dh_msg='您的积分不足';
			}
		}
	}
	unset($duoduo);
	$parameter['good']=$good;
	$parameter['huan_type']=$huan_type;
	$parameter['huan_type_arr']=$huan_type_arr;
	$parameter['jifen_dh_status']=$jifen_dh_status;
	$parameter['jifen_dh_msg']=$jifen_dh_msg;
	$parameter['jifenbao_dh_msg']=$jifenbao_dh_msg;
	$parameter['jifenbao_dh_status']=$jifenbao_dh_status;
	$parameter['id']=$id;
	$parameter['form1_url']=u('ajax','huan');
	return $parameter;
}
?>
<?php
$parameter=act_m_view();
extract($parameter);
$webtitle=$webset['title'].'兑换详情';
include(TPLPATH.'/huan/header_duihuan.tpl.php');
$css[]=TPLPATH.'/inc/css/huan.css';
?><style type="text/css">
.dhbutton{ margin-top:5px; margin-left:80px}
.dhbutton button{ float:left;width:100px; border:none; height:28px;background:#FF5151; cursor:pointer; font-weight:bold; font-size:14px; color:#FFF; line-height:26px;}
.dhbutton button:hover{ width:100px; border:none; height:28px;background:#FF7575; cursor:pointer; font-weight:bold; font-size:14px; color:#FFF; line-height:26px;}
.dhbutton button.jifen{ float:left;background:#02C874;}
.dhbutton button.jifen:hover{background:#02DF82;}
</style>

<script type="text/javascript" src="<?=TPLURL?>/js/tooltip.min.js"></script>
<script type="text/javascript" src="js/jquery.validate.js"></script>

<div class="yingdao" align="center">
  <p><b>兑换详情</b><a href="javascript:;" onClick="history.back()" title="返回" class="left"></a> <a href='<?=wap_l('user','index')?>' title="会员中心" class="right"></a> </p></div>  
  
  	<div class="jifendaohang" style="background:#FFFFFF; overflow:hidden">
<div class="tag_nav" >
			<ul>
			<li>商品分类:</li>
				<a href="<?=wap_l('huan','index')?>"><li<?php if($cid==0){?> class="active"<?php }?>>全部</li></a>
				<?php foreach($huan_type as $catid=>$title){?>
				<a href="<?=wap_l('huan','index',array('cid'=>$catid))?>"><li<?php if($cid==$catid){?> class="active"<?php }?>><?=$title?></li></a>
				<?php }?>
			</ul>
		</div></div>
		
 
 <div class="duihuan">
			<div class="gift-detail">
					<h3 class="gift-name"><?=$good["title"]?></h3>
			<div class="gift-img"><img src="<?=$good['img']?>"  border="0" alt="<?=$good['title']?>" /></div>
			<div class="gift-info">
				<ul>
					<li>
						<strong>积分:</strong><?php if($good['jifen']>0){?>
						<span class="ys1"><?=jfb_data_type($good["jifen"])?> 积分</span>
						<?php }else{?>
						不参与
						<?php }?>
					</li>
					<li>
						<strong><?=TBMONEY?>:</strong><?php if($good['jifenbao']>0){?>
						<span class="ys2"><?=jfb_data_type($good["jifenbao"])?> <?=TBMONEY?></span>	
						<?php }else{?>
						不参与
						<?php }?>
					</li>
					<li>
						<strong>剩余数量:</strong><?=jfb_data_type($good["num"])?> 份
					</li>
					<?php if($good['sdate']>0){?>
					<li>
						<strong>开始时间：</strong><?php echo date('Y-m-d',$good['sdate']);?>
					</li>
					<?php }?>
					<?php if($good['edate']>0){?>
					<li>
						<strong>结束时间：</strong><?php echo date('Y-m-d',$good['edate']);?>
					</li>
					<?php }?>
				</ul></div></div>
				<div align="center"><div class="dhbutton">
<?php if($dduser['name']!=''){?>
	<?php if($good['jifenbao']>0){?>
	<button class="money"  id="jifenbao" title="<?=$jifenbao_dh_msg?>" <?php if($jifenbao_dh_status==0){?> jinzhi="<?=$jifenbao_dh_msg?>"<?php }else{?>jinzhi="0"<?php }?>><?=TBMONEY?>兑换</button>
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<?php }?>
	<?php if($good['jifen']>0){?>
	<button class="jifen"  id="jifen" title="<?=$jifen_dh_msg?>" <?php if($jifen_dh_status==0){?> jinzhi="<?=$jifen_dh_msg?>"<?php }else{?>jinzhi="0"<?php }?>>积分兑换</button>
	<?php }?>
<?php }else{?>
	<?php if($good['jifenbao']>0){?>
	<button class="money" onclick="alert('登陆后才可兑换商品！');window.location='<?=u('user','login')?>&url='+encodeURIComponent(window.location.href)" jinzhi="1"><?=TBMONEY?>兑换</button>
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<?php }?>
	<?php if($good['jifen']>0){?>
	<button class="jifen" onclick="alert('登陆后才可兑换商品！');window.location='<?=u('user','login')?>&url='+encodeURIComponent(window.location.href)" jinzhi="1">积分兑换</button>
	<?php }?>
<?php }?>
</div></div>
		
 <div class="clear"></div>
 
<h4 class="gift-title">申请兑换</h4>
		<div class="contain">
         <form id="form1" name="form1" method="post" action="<?=u('ajax','huan')?>">
                        <table width="350" border="0" cellpadding="0" cellspacing="0">
                        <tr>
                            <td width="120" height="40" align="right">兑换数量：</td>
                            <td width="150" align="left"><input name="num" type="text" class="ddduihuan" value="1" id="num"  /></td>
                          </tr>
                          <tr>
                            <td width="" height="40" align="right">姓名：</td>
                            <td width="150" align="left">
                            <?php if($dduser['realname']!=''){echo $dduser['realname'];}else{?>
                            <input name="realname" type="text" class="ddduihuan" value="<?=$dduser['realname']?>" id="realname" />
                            <?php }?>
                            </td>
                          </tr>
						  <tr>
                            <td width="" height="40" align="right">手机：</td>
                            <td align="left">
                            <?php if($tixian_sms_open==1 || $dduser['mobile']!=''){echo $dduser['mobile'];}else{?>
                            <input name="mobile" type="text" class="ddduihuan" value="<?=$dduser['mobile']?>" id="mobile" />
                            <?php }?>
                            </td>
                          </tr>
						  <tr>
                            <td height="40" align="right">邮箱：</td>
                            <td align="left">
                            <?php if($dduser['email']!=''){?><div style="width:170px; overflow:hidden"><?=$dduser['email']?></div><?php }else{?>
                            <input name="email" type="text" class="ddduihuan" value="<?=$dduser['email']?>" id="email" />
                            <?php }?>
                            </td>
                          </tr>
                          <tr>
                            <td width="" height="40" align="right">支付宝：</td>
                            <td align="left">
                            <?php if($dduser['alipay']!=''){echo $dduser['alipay'];}else{?>
                            <input name="alipay" type="text"  class="ddduihuan"  value="<?=$dduser['alipay']?>" id="alipay"  />
                            <?php }?>
                            </td>
                          </tr>
						  <tr>
                            <td width="" height="40" align="right">QQ：</td>
                            <td align="left">
                            <?php if($dduser['qq']!=''){echo $dduser['qq'];}else{?>
                            <input name="qq" type="text" class="ddduihuan" value="<?=$dduser['qq']?>" id="qq" />
                            <?php }?>
                            </td>
                          </tr>
                          <tr>
                            <td width="" height="40" align="right">地址：</td>
                            <td align="left"><input name="address" type="text" class="ddduihuan" value="" id="address"/></td>
                          </tr>
						  <tr>
                            <td width="" height="40" align="right">备注：</td>
                            <td align="left"><textarea name="content" class="ddduihuan" id="content" style="width:140px;height:44px;line-height:22px;"></textarea></td>
                          </tr>
						  <tr>
                            <td width="" height="40" align="right"></td>
                            <td align="left"><input type="hidden" id="mode" name="mode" value="1" /><input type="hidden" id="id" name="id" value="<?=$id?>" /><input type="submit" value="提 交" class="ShiftClass"  style="color:#FFFFFF; font-size:16px"/></td>
                          </tr>
      </table>
        </form>
</div>

	<div class="jf_xx">
<h4 class="gift-title">礼品详情</h4>
		<div class="gift-content"><?php echo $good['content'];?></div>
	<h4 class="gift-title">注意事项</h4>
		<div class="gift-content">
			<ol style="font-size:14px; line-height:30px">
				<li>1. 所有礼品（除优惠券等外）在兑换后的1-2个工作日，本站将从合作商城购买，具体到货时间取决于合作商城发货情况。礼品发货后，我们将会发送站内信告知，请您留意；</li>
				<li>2. 若兑换礼品缺货或收货地址无法送达，我们会在1~2个工作日将相应返利或积分返还至您的账户中；</li>
				<li>3. 礼品兑换已包含快递费用；</li>
				<li>4. 只能使用购物返利或积分进行礼品兑换；</li>
				<li>5. 本礼品为本站从合作商城采购，为操作方便，下单后所产生的差价双方均无需承担；</li>
			</ol>
		</div>
		</div>
		
</div>
<script>
$(function(){	
    $('.dhbutton button').jumpBox({  
	    title: '<?=$good['title']?>',
		LightBox:'show',
		height:400,
		width:450,
		defaultContain:1,
		jsCode:'var jinzhi=$(this).attr("jinzhi");if(jinzhi!="0"){ if(jinzhi!=1){alert(jinzhi);}var jumpBoxStop=1;};if($(this).attr("id")=="money"){$("#form1 #mode").val(1)}if($(this).attr("id")=="jifen"){$("#form1 #mode").val(2)}'
    });
	$('#form1').validate({
        errorPlacement: function(error, element){
            var error_td = element.parent('td').next('td');
            error_td.find('.field_notice').hide();
            error_td.append(error);
        },
        success       : function(label){
            label.addClass('validate_right').text('OK!');
        },
        onkeyup: false,
        rules : {
			num : {
                required : true,
                range:[1,<?php echo $good['limit'];?>]
            },
            realname : {
                required : true
            },
            mobile : {
                required : true,
                mobile   : true
            },
			<?php if(empty($dduser['alipay'])){?>
			alipay : {
                alipay    : true
            },
			<?php }?>
            email : {
                required : true,
                email    : true
            },
            qq : {
                required : true,
                range:[1000,999999999999]
            }
        },
        messages : {
			num : {
                required : '填写您兑换数量',
                range:'数量最多兑换<?=$good['limit']?>件'
            },
			realname : {
                required : '填写姓名'
            },
            mobile  : {
                required : '填写手机号码',
                mobile: '手机号码格式错误'
            },
			<?php if(empty($dduser['alipay'])){?>
			alipay : {
                alipay    : '支付宝错误'
            },
			<?php }?>
            email : {
                required : '填写电子邮箱',
                email    : '邮箱错误'
            },
			qq : {
                required : '填写您的QQ号码',
                range:'QQ号码位数错误'
            }
        },
		submitHandler: function(form) {
            $form=$('#form1');
			$form.find('.ShiftClass').attr('disabled','disabled');
            var query=$form.serialize();//document.write(query);
	        var url=$form.attr('action');
	        $.ajax({
		        url: url,
		        data:query,
		        dataType:'jsonp',
				jsonp:"callback",
		        success: function(data){
			        if(data.s==0){
			            alert(errorArr[data.id]);
						$form.find('.ShiftClass').attr('disabled',false);
						jumpboxClose();
			        }
			        else if(data.s==1){
			            alert('兑换成功,等待管理员审核');
						location.replace(location.href);
			        }
					else if(data.s==2){
			            alert('兑换成功,请查收站内信领取');
						window.location.href='<?=wap_l('user','msg')?>';
			        }
		        }
	        });
        } 
    });
});
</script>
<?php
include(TPLPATH."/inc/footer.tpl.php");
?>
